package com.jkc.xh.network;

import com.jkc.xh.MainApplication;
import com.jkc.xh.R;
import com.squareup.okhttp.OkHttpClient;

import java.io.InputStream;
import java.security.KeyStore;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

/**
 * Created by wangjie28 on 2015/12/25.
 */
public class ApiOkHttpClient {

    public static OkHttpClient getClient() {
        return OkHttpClientHolder.client;
    }

    private static final class OkHttpClientHolder {
        public final static OkHttpClient client = getHttpClient();

        static {
            RequestHeaderInterceptor headerInterceptor = new RequestHeaderInterceptor();
            //日志
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            client.interceptors().add(headerInterceptor);
            client.interceptors().add(interceptor);
        }

        private static OkHttpClient getHttpClient() {
            try {
//                // Create a trust manager that does not validate certificate chains
//                final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
//                    @Override
//                    public void checkClientTrusted(
//                            java.security.cert.X509Certificate[] chain,
//                            String authType) throws CertificateException {
//                    }
//
//                    @Override
//                    public void checkServerTrusted(
//                            java.security.cert.X509Certificate[] chain,
//                            String authType) throws CertificateException {
//                    }
//
//                    @Override
//                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
//                        return null;
//                    }
//                }};
                KeyStore localTrustStore = KeyStore.getInstance("BKS");
                InputStream input = MainApplication.getInstance().getResources().openRawResource(R.raw.keystore);
                try {
                    localTrustStore.load(input, "890923".toCharArray());
                } finally {
                    input.close();
                }

                CustomTrustManager trustManager = new CustomTrustManager(localTrustStore);

                // Install the all-trusting trust manager
                final SSLContext sslContext = SSLContext.getInstance("SSL");
                sslContext.init(null, new TrustManager[]{trustManager},
                        new java.security.SecureRandom());
                // Create an ssl socket factory with our all-trusting manager
                final SSLSocketFactory sslSocketFactory = sslContext
                        .getSocketFactory();

                OkHttpClient okHttpClient = new OkHttpClient();
                okHttpClient.setSslSocketFactory(sslSocketFactory);
//                okHttpClient.setHostnameVerifier(new HostnameVerifier() {
//
//                    @Override
//                    public boolean verify(String hostname, SSLSession session) {
////                    for (String host : HostKey.HOST_MAP.values()) {
////                        if (hostname.startsWith(host)) {
////                            return true;
////                        }
////                    }
//                        return true;
//                    }
//                });

                return okHttpClient;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }
    }
}
